package com.etime.perfectschool.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.pojo.SchoolActivies;
import com.etime.perfectschool.service.SchoolActiviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
public class SchoolActiviesController {
    @Autowired
    private SchoolActiviesService schoolActiviesService;
    //查询全部活动
    @GetMapping("/getAllSchoolActivies")
    public Page<SchoolActivies> getAllSchoolActivies(int current,int size )
    {

        return schoolActiviesService.getAllSchoolActivies(current,size);
    }
    //发布活动
    @PostMapping("/addSchoolActivies")
    public int addSchoolActivies(SchoolActivies schoolActivies)
    {
        System.out.println(schoolActivies);
        return schoolActiviesService.addSchoolActivies(schoolActivies);
    }

    //删除活动
    @DeleteMapping("/deleteSchoolActivies")
    public boolean deleteSchoolActivies(int activityId)
    {
        return schoolActiviesService.deleteSchoolActivies(activityId);
    }
    //点赞功能的实现
    @PutMapping("/likeSchoolActivies")
    public SchoolActivies likeSchoolActivies(int activityId)
    {
        int count=0;
        SchoolActivies schoolActivies = schoolActiviesService.getSchoolActivies(activityId);
        count=schoolActivies.getActivityUpvote();
        if(count==0)
        {
            schoolActivies.setActivityUpvote(1);
        }else {
            count=count+1;
            schoolActivies.setActivityUpvote(count);
        }
        boolean res = schoolActiviesService.likeSchoolActivies(schoolActivies);
        if(res)
        {
            schoolActivies = schoolActiviesService.getSchoolActivies(activityId);
        }
        return schoolActivies;
    }
    //查询某条新闻
    @GetMapping("/getSchoolActivies")
    public SchoolActivies getSchoolActivies(int activityId)
    {
        return schoolActiviesService.getSchoolActivies(activityId);
    }

    //修改活动
    @PutMapping("/updateSchoolActivies")
    public boolean updateSchoolActivies(SchoolActivies schoolActivies)
    {

        return schoolActiviesService.updateSchoolActivies(schoolActivies);
    }
}
